<!--注册界面-->
<?php
$conn=new mysqli("localhost","root","123456","htmlkeshe");
$conn->query("set names utf8");
//各个内容的确定 php中验证一次但是并没有返回
function checkuname($str){
    $length=strlen($str);
    if($length>16||$length<4){
        echo 1;
        return false;
    }
    //对每一个通过字符过滤条件来防注入   暂时只支持数字字母下划线
    for($i=0;$i<$length;$i++){
        if((ord($str[$i])<=122&&ord($str[$i])>=97)||(ord($str[$i])<=90&&ord($str[$i])>=65)||(ord($str[$i])<=57&&ord($str[$i])>=48)||$str[$i]=="_"){

        }else{
            return false;
        }
    }
    global $conn;
    $result=$conn->query("select name from user where name='".$str."'") or die(mysqli_error($conn));//确认姓名存在
    if(mysqli_num_rows($result)){
        return false;
    }
    return true;
}
function checkpw($str){
    $length=strlen($str);
    if($length>16||$length<4)
    	return false;

    for($i=0;$i<$length;$i++){
//增加过滤条件防止非法输入
        if(ord($str[$i])<48){
            global $hk;
            $hk = false;
            return false;
        }
    }
    return true;
}
function checktel($str){
    $f ="/^1\d{10}$/";
    if (preg_match($f,$str)) {

        return true;
    }else{
        return false;
    }
}
function checkemail($str){
    $f = "/^[a-z0-9]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$/";
    if(preg_match($f,$str)){//正则表达式
        return true;
    }else{
        return false;
    }
}

//submit的确定 运行所有函数并且得到是否有问题
if(isset($_POST["submit"])){
    $check=true;//确定所有是否完全正确

    //姓名是否存在并合法
    if(!checkuname($_POST["name"])){//调用php代码中的函数
        $cuname=false;
        $check=false;

    }
    //比较密码是否一致
    if($_POST["password"]!=$_POST["password_confirm"]){
        $ccpw=false;
        $check=false;
        //echo ("a");
    }
    //密码是否符合格式
    if(!checkpw($_POST["password"])){
        $cpw=false;
        $check=false;
        //echo ("b");
    }
    //email检验
    if(!checkemail($_POST["email"])){
        $cmail=false;
        $check=false;
        //echo ("c");
    }
    //手机检验
    if(!checktel($_POST["phone_number"])){
        $ctel=false;
        $check=false;
        //echo ("d");
    }
}

//在此处写入数据库
if(isset($check)&&$check){
    //先使用文本变量写入再去查询
    $sql0="show table status where `name`='user';";
    $result0=$conn->query($sql0) or die(mysqli_error($conn));
    if($result0->num_rows>0) {
        while ($row = $result0->fetch_assoc()) {
            $num = $row['Auto_increment'];
        }
    }
    $picurl0="./resource/img/userhead/default.jpg";
    $picurl1="./resource/img/userhead/user".$num.".jpg";
    copy($picurl0,$picurl1);


    $name = $_POST["name"];
    $password =MD5($_POST["password"]);
    $email =$_POST["email"];
    $phone_number =$_POST["phone_number"];
    $time =date("Y-m-d H:i:s");
    $sql = "INSERT INTO user(name,head,password,email,phone,time)VALUES
        ('".$name."','".$picurl1."','".$password."','".$email."','".$phone_number."','".$time."')";
//使用md5加密密码 在验证时也进行加密后比较
    $result=$conn->query($sql) or die(mysqli_error($conn));
//die函数输出一条信息并退出当前脚本
    $conn->close();
    $out=<<<EOF
    <style>
            @import url(resource/css/zhuceCSS.css);
        </style>
    <div id="zucechenggong" style="text-align: center">
    <div class="left">
        <p id="cat_p">
            "注册成功了喵"
        </p>
    </div>
    <div class="cat">
        <img src="./resource/img/cat.png">
    </div>
    <div class="right">
    <a href='index.php'>正在跳转到主界面,若长时间未响应,则点击此处</a>";
    </div>
</div>
EOF;
    echo $out;
    //两秒后跳回主页
    header('Refresh: 2; url=index.php');

}else{
?>
<!--注册表单-->
<html>
    <head>
        <meta charset="utf-8">
        <title>注册</title>
        <style>
            @import url(resource/css/zhuceCSS.css);
        </style>

    </head>
    <body>
        <div id="header" class="zhuce_header">
            <p>欢迎注册</p>
        </div>
        <div class="background">
        <div class="zhuce_page">
            <div class="zhuce_page_form">
                <div class="zhuce_page_form_inner">
                    <form action="" method="post" id="form">
                        <div class="name_pass">
                            <label>名称:</label>
                            <input type="text" id="name" name="name" value="<?php if(isset($_POST["name"])) echo $_POST["name"];?>"/>
                            <p id="name_p"><?php if(isset($cuname)&&$cuname==false){
                                    echo "<span style='color:#f00'>用户名不可用，换一个吧</span>";} else{ ?>
                                    长4-16个字符(只可使用数字、字母、下划线)
                                <?php }?></p>
                        </div>
                        <div class="name_pass">
                            <label>密码:</label>
                            <input type="password" id="password" name="password" value="<?php if(isset($_POST["pass"])) echo $_POST["pass"];?>"/>
                            <p id='password_p'><?php if(isset($hk)&&$hk==false){echo "<span style='color:#f00'>密码中含有非法字符</span>";}
                                     else if (isset($cpw)&&$cpw==false){echo "<span style='color:#f00'>密码长度4-16个字符</span>";} else{?>
                                    请输入密码
                                <?php }?></p>
                        </div>
                        <div class="name_pass">
                            <label>重复:</label>
                            <input type="password" id="password_confirm" name="password_confirm" value="<?php if(isset($_POST["conpass"])) echo $_POST["conpass"];?>"/>
                            <p id="password_confirm_p">再次输入相同的密码</p>
                        </div>
                        <div class="name_pass">
                            <label>邮箱:</label>
                            <input type="text" id="email" name="email" value="<?php if(isset($_POST["email"])) echo $_POST["email"];?>"/>
                            <p id="email_p"><?php if(isset($cmail)&&$cmail==false){
                                    echo "<span style='color:#f00'>邮箱格式不正确</span>";} else{ ?>
                                    输入您的邮箱
                                <?php }?></p>
                        </div>
                        <div class="name_pass">
                            <label>手机:</label>
                            <input type="text" id="phone_number" name="phone_number" value="<?php if(isset($_POST["phone_number"])) echo $_POST["phone_number"];?>"/>
                            <p id="phone_number_p"><?php if(isset($ctel)&&$ctel==false){
                                    echo "<span style='color:#f00'>这个号码联系不到你啊</span>";} else{ ?>
                                    输入您的手机号码
                                <?php }?></p></br>
                        </div>
                        <div class="submit">
                            <input type="submit" id="submit" value="提交" name="submit"/>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        </div>
        <script src="resource/js/zhuce_check.js"></script>
    </body>
</html>

<?php }
?>
